Imports System
Imports System.Text

Public Class ProductEditView
    Implements IUrlSerializer

    Private _productId As String
    Private _title As String
    Private _description As String
    Private _quantityOnHand As String
    Private _price As String
    Private _releaseDate As String

    Public Property ProductId() As String
        Get
            Return Me._productId
        End Get
        Set(ByVal value As String)
            Me._productId = value
        End Set
    End Property

    Public Property Title() As String
        Get
            Return Me._title
        End Get
        Set(ByVal value As String)
            Me._title = value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return Me._description
        End Get
        Set(ByVal value As String)
            Me._description = value
        End Set
    End Property

    Public Property QuantityOnHand() As String
        Get
            Return Me._quantityOnHand
        End Get
        Set(ByVal value As String)
            Me._quantityOnHand = value
        End Set
    End Property

    Public Property Price() As String
        Get
            Return Me._price
        End Get
        Set(ByVal value As String)
            Me._price = value
        End Set
    End Property

    Public Property ReleaseDate() As String
        Get
            Return Me._releaseDate
        End Get
        Set(ByVal value As String)
            Me._releaseDate = value
        End Set
    End Property

    Public Function SerializeToEncodedUrlString() As String Implements IUrlSerializer.SerializeToUrlString
        Dim sb As New StringBuilder()
        sb.AppendFormat("productId={0}&", Me.ProductId)
        sb.AppendFormat("title={0}&", Me.Title)
        sb.AppendFormat("description={0}&", Me.Description)
        sb.AppendFormat("quantityonhand={0}&", Me.QuantityOnHand)
        sb.AppendFormat("price={0}&", Me.Price)
        sb.AppendFormat("releasedate={0}", Me.ReleaseDate)
        Return sb.ToString()
    End Function

    Public Shared Function ToProductEditView(ByVal product As Product) As ProductEditView
        Dim view As New ProductEditView()
        view.ProductId = product.ProductId.ToString()
        view.Title = product.Title
        view.Description = product.Description
        view.Price = String.Format("{0:c}", product.Price)
        view.QuantityOnHand = product.QuantityOnHand.ToString()
        view.ReleaseDate = product.ReleaseDate.ToShortDateString()
        Return view
    End Function

    Public Shared Function ToProduct(ByVal view As ProductEditView) As Product
        Dim db As Double
        Dim num As Integer
        Dim dt As DateTime

        Dim product As New Product()
        product.Title = view.Title
        product.Description = view.Description

        If Double.TryParse(view.Price.TrimStart("$"c), db) Then
            product.Price = db
        Else
            Throw New ArgumentException("Price is malformed or not a valid value.")
        End If

        If Integer.TryParse(view.ProductId, num) Then
            product.ProductId = num
        Else
            Throw New ArgumentException("ProductId is malformed or not a valid value.")
        End If

        If Integer.TryParse(view.QuantityOnHand, num) Then
            product.QuantityOnHand = num
        Else
            Throw New ArgumentException("QuantityOnHand is malformed or not a valid value.")
        End If

        product.ProductId = Convert.ToInt32(view.ProductId)
        product.QuantityOnHand = Convert.ToInt32(view.QuantityOnHand)

        product.ReleaseDate = Convert.ToDateTime(view.ReleaseDate)

        If DateTime.TryParse(view.ReleaseDate, dt) Then
            product.ReleaseDate = dt
        Else
            Throw New ArgumentException("ReleaseDate is malformed or not a valid value.")
        End If

        Return product
    End Function

End Class